package com.ruoyi.legal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Pack;
import com.ruoyi.legal.service.IPackService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 打包备份Controller
 * 
 * @author xwj
 * @date 2025-05-10
 */
@RestController
@RequestMapping("/legal/pack")
@Api(tags = "打包备份")
public class PackController extends BaseController {
    @Autowired
    private IPackService packService;

    /**
     * 查询打包备份列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Pack pack) {
        startPage();
        List<Pack> list = packService.selectPackList(pack);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Pack pack) {
        startOrderBy();
        List<Pack> list = packService.selectPackList(pack);
        return success(list);
    }

    /**
     * 导出打包备份列表
     */
    @Log(title = "打包备份", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Pack pack) {

        if(pack.getType() == 1) {
            packService.exportPackAjtz(response, pack.getId());
        } else if(pack.getType() == 2){
            packService.exportPackYstz(response, pack.getId());
        } else {
            packService.exportPackXjcl(response, pack.getId());
        }
    }


    /**
     * 获取打包备份详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(packService.selectPackById(id));
    }

    @PostMapping("/add")
    @ApiOperation("添加备份")
    public AjaxResult add(@RequestBody Pack pack) {
        SysUser user = getLoginUser().getUser();

        pack.setCreateId(user.getUserId());
        pack.setCreateBy(user.getNickName());

        packService.addPack(pack);

        return success(pack);
    }
    /**
     * 新增打包备份
     */
    @Log(title = "打包备份", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Pack pack) {
        SysUser user = getLoginUser().getUser();
        if(pack.getId() == null) {
            pack.setCreateId(user.getUserId());
            pack.setCreateTime(DateUtils.getNowDate());
            pack.setCreateBy(user.getNickName());
            packService.insertPack(pack);
        } else {
            pack.setUpdateId(user.getUserId());
            pack.setUpdateTime(DateUtils.getNowDate());
            pack.setUpdateBy(user.getNickName());
            packService.updatePack(pack);
        }

        return success(pack);
    }

    /**
     * 修改打包备份
     */
    @Log(title = "打包备份", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Pack pack) {
        SysUser user = getLoginUser().getUser();
                pack.setUpdateId(user.getUserId());
                pack.setUpdateTime(DateUtils.getNowDate());
                pack.setUpdateBy(user.getNickName());
        return success(packService.updatePack(pack));
    }

    /**
     * 删除打包备份
     */
    @Log(title = "打包备份", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(packService.deletePackByIds(ids));
    }

    @Log(title = "打包备份", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Pack pack) {
        int num = packService.delete(pack);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Pack>> queryPage(@RequestBody Pack pack) {
        Pages<Pack> page = packService.queryPage(pack);
        return ResponseModel.of(page);
    }
}
